package com.market.plus.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.lang.time.DateUtils;

public class DateUtil {

  public static final String LONG_COMMON_PATTERN = "yyyy-MM-dd hh:mm:ss z(ZZZZ) aaa";

  public static final String SHORT_COMMON_PATTERN = "MM-dd-yyyy";

  public static final String COMMON_TIMEZONE_ID = "GMT+00:00";

  public static Long getCurrentTime(){
     return System.currentTimeMillis();
  }

  public static Date getCurrentDate(){
     Date currentDate = new Date(getCurrentTime());
     return currentDate;
  }

  public static Date convertDate(String date) throws ParseException{
     SimpleDateFormat formatter = new SimpleDateFormat(LONG_COMMON_PATTERN);
     formatter.setTimeZone(TimeZone.getTimeZone(COMMON_TIMEZONE_ID));
     return formatter.parse(date);
  }
  
  public static Calendar convertCalendar(String date) throws ParseException{
     Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(COMMON_TIMEZONE_ID));
     Date convertDate = convertDate(date);
     calendar.setTime(convertDate);
     return calendar;
  }

  public static Calendar convertCalendar(Date date) throws ParseException{
     Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(COMMON_TIMEZONE_ID));
     calendar.setTime(date);
     return calendar;
  }

  public static String formatShortDate(Date date, int timezoneOffsetMinutes) throws ParseException{
     SimpleDateFormat formatter = new SimpleDateFormat(SHORT_COMMON_PATTERN);
     TimeZone timeZone = TimeZone.getTimeZone(DateUtil.COMMON_TIMEZONE_ID);
     timeZone.setRawOffset((int) (timezoneOffsetMinutes * DateUtils.MILLIS_PER_MINUTE));
     formatter.setTimeZone(timeZone);
     return formatter.format(date);
  }

  public static String formatLongDate(Date date, int timezoneOffsetMinutes) throws ParseException{
     SimpleDateFormat formatter = new SimpleDateFormat(LONG_COMMON_PATTERN);
     TimeZone timeZone = TimeZone.getTimeZone(DateUtil.COMMON_TIMEZONE_ID);
     timeZone.setRawOffset((int) (timezoneOffsetMinutes * DateUtils.MILLIS_PER_MINUTE));
     formatter.setTimeZone(timeZone);
     return formatter.format(date);
  }

  public static Date convertDate(Calendar calendar) throws ParseException{
     return calendar.getTime();
  }

  public static String getTimeZoneId(int timezoneOffsetMinutes){
     TimeZone timeZone = TimeZone.getTimeZone(DateUtil.COMMON_TIMEZONE_ID);
     timeZone.setRawOffset((int)(timezoneOffsetMinutes * DateUtils.MILLIS_PER_MINUTE));
     return timeZone.getID();
  }
}
